#!/bin/bash
## We should firstly confirm the repo info is the latest
while true;do
sudo apt update
if [ "$?" = "0" ];then
break
else
sleep 10
echo "aptss is locked. Will try it later in 10 secs"
fi
done
REALPATH=`realpath $0`
APP_IN_STORE_PATH=`dirname $REALPATH`
pkgname=org.mozilla.firefox-nal
archi=amd64
VERSION_FIREFOX_IN_REPO=`apt show org.mozilla.firefox-nal | grep -oP 'Version: \K\S+'`
VERSION_FIREFOX_IN_MOZILLA=`curl -fI 'https://download.mozilla.org/?product=firefox-latest&os=linux64&lang=zh-CN' | grep -o 'firefox-[0-9.]\+[0-9]' | sed 's/.*firefox-//'`
if dpkg --compare-versions "$VERSION_FIREFOX_IN_REPO" "<<" "$VERSION_FIREFOX_IN_MOZILLA";then
## Here we give the value to version
version=$VERSION_FIREFOX_IN_MOZILLA
WORKDIR=`mktemp -d`
cd $WORKDIR
mkdir -p pkgdir/DEBIAN
cd pkgdir
### Write Control
cat << EOFFFFFF > DEBIAN/control
Package: ${pkgname}
Version: ${version}
Installed-Size: 214784
Maintainer: shenmo <shenmo@spark-app.store>
Homepage: https://www.mozilla.org/zh-CN/firefox/all/#product-desktop-release
Architecture: $archi
Description: 一款开源自由的浏览器，现在由星火自动维护系统（测试版）维护中...
Section: web
Priority: optional
Depends: libdbus-glib-1-2
Recommends: speech-dispatcher
Provides: x-www-browser,gnome-www-browser,firefox(=${version})
Replaces: firefox

EOFFFFFF
chmod 755 DEBIAN/control

mkdir -p opt/apps/org.mozilla.firefox-nal/entries/applications
mkdir -p opt/apps/org.mozilla.firefox-nal/entries/icons
### Write desktop
cat << EOFFFFFF > opt/apps/org.mozilla.firefox-nal/entries/applications/org.mozilla.firefox-nal.desktop
[Desktop Entry]
Name=Firefox
Name[zh_CN]=Firefox火狐浏览器
Version=1.0
Exec=/opt/apps/org.mozilla.firefox-nal/files/firefox/firefox %U
Comment=Firefox
Icon=org.mozilla.firefox-nal
Type=Application
Terminal=false
StartupNotify=true
Encoding=UTF-8
Categories=Network;WebBrowser;
MimeType=text/html;text/xml;application/xhtml_xml;application/x-mimearchive;x-scheme-handler/http;x-scheme-handler/https;


EOFFFFFF


### Write info
cat << EOFFFFFF > opt/apps/org.mozilla.firefox-nal/info
{
    "appid": "org.mozilla.firefox-nal",
    "name": "org.mozilla.firefox-nal",
    "version": "${version}",
    "arch": ["amd64"],
    "permissions": {
        "autostart": false,
        "notification": true,
        "trayicon": false,
        "clipboard": true,
        "account": false,
        "bluetooth": true,
        "camera": true,
        "audio_record": true,
        "installed_apps": false
    }
}
EOFFFFFF

mkdir -p opt/apps/org.mozilla.firefox-nal/files

### Download and uncompress firefox latest

Download_Name="fflatest.tar.bz2"
Download_Url="https://download.mozilla.org/?product=firefox-latest-ssl&os=linux64&lang=zh-CN"
aria2c -c -x 16 -s 16  -d "opt/apps/org.mozilla.firefox-nal/files" "$Download_Url" -o "$Download_Name" --summary-interval=1
tar -xvf "opt/apps/org.mozilla.firefox-nal/files"/"$Download_Name" -C "opt/apps/org.mozilla.firefox-nal/files"
rm "opt/apps/org.mozilla.firefox-nal/files"/"$Download_Name"

mkdir -p opt/apps/org.mozilla.firefox-nal/files/firefox/distribution
### Write distribution info and policies
cat << EOFFFFFF > opt/apps/org.mozilla.firefox-nal/files/firefox/distribution/distribution.ini
[Global]
id=flamescion
version=1.0
about=Mozilla Firefox for Spark Store

[Preferences]
app.distributor="Project Spark"
app.distributor.channel="Spark Store"
mozilla.partner.id="flamescion"
EOFFFFFF

cat << EOFFFFFF > opt/apps/org.mozilla.firefox-nal/files/firefox/distribution/policies.json
{
    "policies":
    {
        "DisableAppUpdate": true
    }
}
EOFFFFFF
cp opt/apps/org.mozilla.firefox-nal/files/firefox/browser/chrome/icons/default/default128.png opt/apps/org.mozilla.firefox-nal/entries/icons/org.mozilla.firefox-nal.png

cd $WORKDIR/pkgdir
fakeroot dpkg-deb -Z xz -b . ../${pkgname}_${version}_${archi}.deb
cd $WORKDIR
mv ${pkgname}_${version}_${archi}.deb $APP_IN_STORE_PATH
cd $APP_IN_STORE_PATH
rm -rf $WORKDIR


SIZE=`du -s ./${pkgname}_${version}_${archi}.deb`
SIZE=`echo ${SIZE%%.*}`



else
echo "No need to upgrade, exit"

fi

